package com.lbb.springboot.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lbb.springboot.auto.util.Cfg;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;

/**
 * 用于测试-添加注释
 *
 * @author zhangkeke
 * @since 2017/10/18 15:31
 */
public class WeChatQRCodeUtils {

    //生成安装证明图片

    public static void install(String[] args) throws Exception {

        String orderId="200";
        String filePath="D:\\xcx\\install\\";
//        String filePath = "C:\\Users\\CangMaoMao\\IdeaProjects\\xlulbb\\src\\main\\java\\com\\lbb\\img/";

//        String page="pagesIndex/pages/red-packet/red-packet";
        String page="pages/index/index";
        String wsQRcode = getWXACodeUnlimit(filePath,orderId,page, 280, true, null, true);
        System.out.println("wxaCodeUnlimit:" + wsQRcode);

        //        // 获取类所在项目路径，获取方式有多种
//        String address = Main.class.getResource("/").getPath();
//        String address = "C:\\Users\\CangMaoMao\\Desktop\\shareImg-master\\src\\";
        // 头像
        String avastar = filePath + "install.jpg";
        // 二维码
        String qrcode = filePath + wsQRcode;
        // 二维码识别图
        String point = filePath + "point.png";
        // 背景图片
        String background = filePath + "bg.jpg";

        // 1. 通过背景图片构建 BufferedImage
        BufferedImage zoomPicture = ImageIO.read(new File(background));
        BufferedImage roundedImage = ImageIO.read(new File(avastar));
        // 2. 头像裁剪成圆形
//        BufferedImage roundedImage = SharedImageUtils.createRoundedImage(avastar, SharedImageUtils.AVATAR_SIZE);
        // 3. 合并头像，昵称
        BufferedImage mergeImage = SharedImageUtils.mergePicture(zoomPicture, roundedImage, "长按识别二维码，分享领取现金大红包");
        // 4. 合并二维码及二维码识别图
        mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcode, null);
        // 5. 生成分享图
        String fileName = DateUtil.getNowDate() + UUID.randomUUID().toString().substring(0, 8) +"result.jpg";
        ImageIO.write(mergeImage, "jpg", new File(filePath + fileName));

    }


    public static void main(String[] args) throws Exception {

        String orderId="200";
//        String filePath=null;
        String filePath = "C:\\Users\\CangMaoMao\\IdeaProjects\\xlulbb\\src\\main\\java\\com\\lbb\\img\\";

//        String page="pagesIndex/pages/red-packet/red-packet";
        String page="pages/index/index";
        String wsQRcode = getWXACodeUnlimit(filePath,orderId,page, 280, true, null, true);
        System.out.println("wxaCodeUnlimit:" + wsQRcode);

        //        // 获取类所在项目路径，获取方式有多种
//        String address = Main.class.getResource("/").getPath();
//        String address = "C:\\Users\\CangMaoMao\\Desktop\\shareImg-master\\src\\";
        // 头像
        String avastar = filePath + "op.png";
        // 二维码
        String qrcode = filePath + wsQRcode;
        // 二维码识别图
        String point = filePath + "point.png";
        // 背景图片
        String background = filePath + "install.jpg";

        // 1. 通过背景图片构建 BufferedImage
        BufferedImage zoomPicture = ImageIO.read(new File(background));
        BufferedImage roundedImage = ImageIO.read(new File(avastar));
        // 2. 头像裁剪成圆形
//        BufferedImage roundedImage = SharedImageUtils.createRoundedImage(avastar, SharedImageUtils.AVATAR_SIZE);
        // 3. 合并头像，昵称
        BufferedImage mergeImage = SharedImageUtils.mergePicture(zoomPicture, roundedImage, "长按识别二维码，分享领取现金大红包");
        // 4. 合并二维码及二维码识别图
        mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcode, null);
        // 5. 生成分享图
        String fileName = DateUtil.getNowDate() + UUID.randomUUID().toString().substring(0, 8) +"result.jpg";
        ImageIO.write(mergeImage, "jpg", new File(filePath + fileName));

    }


    public static String getAccessToken() {
        String getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Cfg.Weixin.APPID + "&secret=" + Cfg.Weixin.APPSECRET;
        String s = HttpClientUtils.get(getToken);
        JSONObject jsonObject = JSON.parseObject(s);
        String access_token = jsonObject.get("access_token").toString();
        return access_token;
    }


    public static String getWXACodeUnlimit(String filePath,String scene,String page, Integer width, Boolean autoColor, Map<String, Object> lineColor, Boolean isHyaline) {
        String GET_WXACODE_UNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit";
        JSONObject data = new JSONObject();
        data.put("page", page);
        data.put("scene", scene);
        if (null != width)
            data.put("width", width);
        if (null != autoColor)
            data.put("auto_color", autoColor);
        if (null != lineColor)
            data.put("line_color", JSONObject.toJSON(lineColor).toString());
        if (null != isHyaline)
            data.put("is_hyaline", isHyaline);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> httpEntity = new HttpEntity<String>(data.toJSONString(), headers);
        ResponseEntity<byte[]> response = restTemplate.exchange(GET_WXACODE_UNLIMIT_URL + "?access_token=" + getAccessToken(), HttpMethod.POST,
                httpEntity, byte[].class);
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] body = response.getBody();

        String fileName = DateUtil.getNowDate() + UUID.randomUUID().toString().substring(0, 8) +"qr.jpg";
        //生成二维码图片
        ImgErToFileUtil.saveToImgByStr(body, filePath, fileName);
//        return response != null ? encoder.encodeToString(response.getBody()) : "";
        return  fileName;
    }

}
